<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\UserRequest;
use App\Models\User;
use Illuminate\Http\Request;
use \Illuminate\Support\Facades\Cache;

class UsersController extends Controller
{
    //
    public function store(UserRequest $request)
    {

        $verifyData = Cache::get($request->verification_key);
        if (!isset($verifyData['code']) || !hash_equals($verifyData['code'], $request->verification_code)){
            return $this->response->errorUnauthorized('验证码错误');
        }

        User::create([
            'name' => $request->name,
            'phone' => $verifyData['phone'],
            'password' => bcrypt($request->password),
        ]);
        Cache::forget($request->verification_key);
        return $this->response->created();
    }
}
